(add-to-list 'load-path
	     (expand-file-name "~/.emacs.d/packages/muse-latest/lisp/")
	     t)

(require 'muse-mode)                    ;; load authoring mode
(require 'muse-html)                    ;; load publishing styles I use
(require 'muse-project)

;; 启动就把 C-c C-f 绑定到 muse-project-find-file
(global-set-key "\C-c\C-f" 
		(lambda ()
		  (interactive)
		  (let* ((project (muse-project "wiki"))
			 (default (muse-get-keyword :default (cadr project))))
		    (muse-project-find-file
		     (car (muse-read-project-file
			   project (if default
				       (format "Find page: (default: %s) "
					       default)
				     "Find page: ")
			   default))
		     project))))

;; muse 工程
(require 'muse-project)
(setq muse-project-alist      
      `(
	("wiki"				; Leto's notes      
	 ("~/wiki/source" 
	  :default "index")         
	 (:base "leto-html" :path "~/wiki/html"))))

(add-hook 'muse-mode-hook
	  '(lambda ()
	     (auto-fill-mode t)
	     (footnote-mode)
	     (setq outline-regexp "\\*+ ")
	     (outline-minor-mode)
	     (footnote-mode)
	     (hide-body)))

(define-key muse-mode-map (kbd "C-c C-b") 'muse-browse-result)

;; 引进 org-mode 的一些舒服的键绑定
(require 'org)
(define-key muse-mode-map (kbd "<tab>") 'org-cycle)
(define-key muse-mode-map (kbd "<S-iso-lefttab>") 'org-shifttab)

(muse-derive-style "leto-html" "xhtml"
		   :header "~/wiki/source/template/header.html"
		   :footer "~/wiki/source/template/footer.html"
		   :style-sheet 'leto-muse-html-style-sheet
		   :after 'kid-muse-html-insert-contents)

(setq muse-html-table-attributes
      " class=\"muse-table\"")

(defconst leto-muse-html-style-sheet
  "<link rel=\"stylesheet\" type=\"text/css\" media=\"screen\" href=\"../css/wiki.css\" />"
  "Style-sheet used for publishing leto-html pages. ")

(custom-set-variables
 '(muse-html-meta-content-type "text/html; charset=utf-8")
 '(muse-html-charset-default 'utf-8)
 '(muse-html-encoding-default 'utf-8)
 '(muse-colors-inline-images nil))

(setq muse-colors-autogen-headings 'outline)

(defun kid-muse-html-insert-contents ()
  (let ((max-depth 6)
        (index 1)
        base contents l)
    (save-excursion
      (goto-char (point-min))
      (catch 'done
        (while (re-search-forward "^<h\\([0-9]+\\)>\\(.+?\\)</h\\1>" nil t)
          (unless (get-text-property (point) 'read-only)
            (setq l (1- (string-to-number (match-string 1))))
            (if (null base)
                (setq base l)
              (if (< l base)
                  (throw 'done t)))
            (when (<= l max-depth)
              (setq contents (cons (cons l (muse-match-string-no-properties 2))
                                   contents))
              (goto-char (match-beginning 2))
              (muse-html-insert-anchor (concat "sec" (int-to-string index)))
              (setq index (1+ index)))))))
    (setq index 1 contents (nreverse contents))
    (goto-char (point-min))
    (search-forward "<!--contents goes here-->" nil t)
    (let ((depth 1) (sub-open 0) (p (point)))
      (when contents
	(muse-insert-markup 
	 "<div id=\"toc\"><h2>Table of Contents</h2>\n<ul class=\"parent\">\n")
	(while contents
	  (muse-insert-markup "<li><a href=\"#sec" (int-to-string index) "\">"
			      (cdar contents)
			      "</a></li>\n")
	  (setq index (1+ index)
		depth (caar contents)
		contents (cdr contents))
	  (when contents
	    (cond
	      ((< (caar contents) depth)
	       (let ((idx (caar contents)))
		 (while (< idx depth)
		   (muse-insert-markup "</ul>\n")
		   (setq sub-open (1- sub-open)
			 idx (1+ idx)))))
	      ((> (caar contents) depth) ; can't jump more than one ahead
	       (muse-insert-markup "<ul>\n")
	       (setq sub-open (1+ sub-open))))))
	(while (> sub-open 0)
	  (muse-insert-markup "\n")
	  (setq sub-open (1- sub-open)))
	(muse-insert-markup "</ul></div>\n"))
	(muse-publish-mark-read-only p (point)))))
